<?
$_id_gallery	= D_GALLERY_DATA;

if ($DATA['component']['id'] && ($g = (int)param('gallery'))) {
	$_check = postdata('l01') == $DATA['component']['id'];

	$s = array_extend( array('path' => 'album', 'tn' => FP_TN_DIR, 'w' => 800, 'h' => 600, 'tnw' => 128, 'tnh' => 96, 'imode' => MI_FIT, 'tnmode' => MI_FIT, 'gmode' => MI_FIT, 'gw' => 128, 'gh' => 96, 'gtpb' => 0, 'gparam' => 'gallery', 'param' => 'id', 'show_tn' => 1, 'nav' => 0, 'navigator' => 1, 'circular' => 0, 'autoscroll' => 0, 'fancybox' => 0, 'autogallery' => 0,), $DATA['component']['settings']);

	$s['path'] = createdir(FP_UPLOAD_PATH."$s[path]/$g/");
	createdir($s['path'].$s['tn']);


	if (param('upload') == 'images') {	
		if ($temp = takeImage('Filedata', $s['w'], $s['h'], $s['path'], $s['tnw'], $s['tnh'], $s['path'].$s['tn'], $s['imode'], $s['tnmode'])) {
		
			$key = (substr($s['path'], -1, 1) == '/') ? array(h($s['path']), h(substr($s['path'], 0, -1))) : array(h($s['path']).'/', h($s['path']));
			$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash
	
			$_c = "id_data=$_id_gallery AND assoc IN ($key)";		
			$max = ($m = datafetch('db_item', tb('data'), $_c, 'MAX(id_index) as m')) ? $m['m']+1 : 1;
	
			datafetch('db_insert', tb('data'), array('id_data' => $_id_gallery, 'assoc' => $s['path'], 'id_index' => $max, 'name' => $temp['filename']));
	
			print '/'.$temp['filename'];
		}

		exit;
	}

	if ($_check && $d = param('delete')) {	// *** delete photo
	
		if ((strpos($d, '..') === false) && is_file($_SERVER['DOCUMENT_ROOT'].$s['path'].$d)) {
			
			@unlink($_SERVER['DOCUMENT_ROOT'].$s['path'].$d);
			@unlink($_SERVER['DOCUMENT_ROOT'].$s['path'].$s['tn'].$d);
			
			$key = (substr($s['path'], -1, 1) == '/') ? array(h($s['path']), h(substr($s['path'], 0, -1))) : array(h($s['path']).'/', h($s['path']));
			$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash
			
			dataset('db_delete', tb('data'), "id_data=$_id_gallery AND assoc in ($key) AND name='".h($d)."'");
		}
	
//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
		exit;
	}
	
	
if ($_check && $a = strtolower(trim(param('sort')))) {
	
	switch($a) {
	
		case 'manual':	// ajax request, no data should be returned

//			ob_start();

			$t = 0;

			$key = (substr($s['path'], -1, 1) == '/') ? array(h($s['path']), h(substr($s['path'], 0, -1))) : array(h($s['path']).'/', h($s['path']));
			$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash
			
			$DATA['objects'] = datafetch_array('db_list', array(
				'table'	=> 'data',
				'where'	=> "id_data=$_id_gallery AND assoc IN ($key)",
				'order'	=> 'id_index',
				'index'	=> 'name'
			));
//debug($DATA['objects']);

			parse_str(postdata('order'), $temp);

			if (is_array($temp = $temp["objects"]) && sizeof($temp)) {
//dbdebug();
//debug($temp);


				foreach($temp as $id) {
					if ($id) {
						if ($DATA['objects'][$id]) {
							dataset('db_update', tb('data'), "id_index = ".++$t, "id={$DATA[objects][$id][id]}");
							unset($DATA['objects'][$id]);
						} else {
							dataset('db_insert', tb('data'), array(
								'id_data' => $_id_gallery,
								'id_index' => ++$t,
								'assoc' => $s['path'],
								'name' => $id));
							unset($DATA['objects'][$id]);
						}
					}
				}
				
				if (sizeof($DATA['objects']))
					foreach($DATA['objects'] as $id => $v) 
						dataset('db_update', tb('data'), "id_index = ".++$t, "id=$v[id]");
			}
			
//				$_res['diags'] = ob_get_clean();
//				print(php2js($_res));
			
			exit;	// ajax: no more processing needed
			break;
	}
}
	
	


	$DATA['gallery'] = datafetch_array('gallery2', $s['path']);	// load existing images list
//debug(	$DATA['gallery']);
//	uksort($DATA['gallery'], 'fn_numeric_compare');
	
	switch (action(array('photo_add'))) {
	
		case 'photo_add':

			if ($temp = takeImage('image', $s['w'], $s['h'], $s['path'], $s['tnw'], $s['tnh'], $s['path'].$s['tn'], $s['imode'], $s['tnmode'])) {
				
				$key = (substr($s['path'], -1, 1) == '/') ? array(h($s['path']), h(substr($s['path'], 0, -1))) : array(h($s['path']).'/', h($s['path']));
				$key = "'".implode("','", $key)."'";	// 2 versions of path -  with and without trailing slash
	
				$_c = "id_data=$_id_gallery AND assoc IN ($key)";		
				$max = ($m = datafetch('db_item', tb('data'), $_c, 'MAX(id_index) as m')) ? $m['m']+1 : 1;

				datafetch('db_insert', tb('data'), array('id_data' => $_id_gallery, 'assoc' => $s['path'], 'id_index' => $max, 'name' => $temp['filename']));
		
				$DATA['gallery'][$temp['filename']] = array('w' => $temp['w'], 'h' => $temp['h'], 'fn' => $temp['filename'], 'tn' => $s['tn'].$temp['filename']);
			}
	
//			if ($temp = takeImage('image', $s['w'], $s['h'], $s['path'], $s['tnw'], $s['tnh'], $s['path'].$s['tn']))
//					$DATA['gallery'][$temp['filename']] = array('w' => $temp['w'], 'h' => $temp['h'], 'fn' => $temp['filename'], 'tn' => $s['tn'].$temp['filename']);
					
			return false;
	}


	return false;

} else exit;